<?php


namespace Index;
use Library\View as view;
use Model\User;
use Slim\Http\Request;
use Illuminate\Database\Capsule\Manager as DB;

defined('slim') or exit('访问不合法');
class LoginController
{
    
    //登陆页
    public function index($request,$response,$args)
    {
        return view::render([],'login_page');
    }
    //登陆验证页
    public function login($request,$response,$args)
    {
        try {
            //
            $post = $request->getParsedBody();
            $user_name = trim($post['username']);
            $pwd = trim($post['pwd']);
            $res = User::where([
                ['user_name',$user_name],
                ['user_pwd',$pwd]
            ])->first();
            dd($res);
            if (!empty($res)) {
                setcookie('member_id',$res->user_id,time()+(60*60*8));
                $_SESSION['member_name'] = $res->user_name;
                $_SESSION['member_id'] = $res->user_id;
                header("location:/");
            } else {
                header("location:".$request->getServerParams()['HTTP_REFERER']);
            }
        } catch (\Exception $e) {
            dd($e->getMessage());
            return ['code' => 400, 'msg' => $e->getMessage()];
        } catch (\Throwable $t) {
            dd($t->getMessage());
            return ['code' => 400, 'msg' => $t->getMessage()];
        }

    }

}